package com.quick.service.impl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.http.client.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.quick.config.QuickFtpProperties;
import com.quick.service.IFtpService;
import com.quick.util.FtpUtil;
import com.quick.util.ImageTool;

import net.coobird.thumbnailator.Thumbnails;
@Service
public class FtpServiceImpl implements IFtpService{
	
	@Resource
	private QuickFtpProperties ftpProperties;

	@Override
	public Map<String, Object> uploadFtp(MultipartFile multipartFile,Map<String, Object>  base) {
		Map<String, Object> result = new HashMap<String, Object>();
		String oldFileName = multipartFile.getOriginalFilename();
		String newFileName = System.currentTimeMillis()+"-"+UUID.randomUUID();
		String substring = oldFileName.substring(oldFileName.lastIndexOf("."));
		newFileName = newFileName + substring;
		String filePath =  DateUtils.formatDate(new Date(), "yyyy/MM/dd");
		try {
			InputStream inputStream = null;
			String type = base.get("type").toString();
			if("image".contentEquals(type)) {
				String original = base.get("sizeType").toString();
				if("shear".contentEquals(original)) {
					Integer width = Integer.parseInt(base.get("width").toString());
					Integer height = Integer.parseInt(base.get("height").toString());
					newFileName = System.currentTimeMillis()+"-"+width+"x"+height+substring;
					BufferedImage bufferedImage = Thumbnails.of(multipartFile.getInputStream()).size(width,height).asBufferedImage();
					inputStream = ImageTool.bufferedImageToInputStream(bufferedImage);
				}
				if("compress".contentEquals(original)) {
					inputStream = multipartFile.getInputStream();
				}
				
			}else {
				inputStream = multipartFile.getInputStream();
			}
			Boolean res = FtpUtil.uploadFile(ftpProperties.getHost(), ftpProperties.getPort(), ftpProperties.getUserName(), ftpProperties.getPassword(), ftpProperties.getBasePath(), filePath, newFileName, inputStream);
			if(res) {
				String http = ftpProperties.getHttpPath()+ ftpProperties.getBasePath() + filePath + "/"+newFileName;
				result.put("url", http);
				result.put("domain", ftpProperties.getHttpPath());
				result.put("filePath", ftpProperties.getBasePath() + filePath + "/"+newFileName);
				result.put("state", true);
				return result;
			}
			result.put("state", false);
			return result;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Map<String, Object> multiFileUpload(MultipartFile[] multipartFiles,Map<String, Object>  base) {
		Map<String, Object> result = new HashMap<String, Object>();
		for (MultipartFile multipartFile : multipartFiles) {
			String oldFileName = multipartFile.getOriginalFilename();
			String newFileName = System.currentTimeMillis()+"-"+UUID.randomUUID();
			String substring = oldFileName.substring(oldFileName.lastIndexOf("."));
			newFileName = newFileName + substring;
			String filePath =  DateUtils.formatDate(new Date(), "yyyy/MM/dd");
			try {
				Boolean res = FtpUtil.uploadFile(ftpProperties.getHost(), ftpProperties.getPort(), ftpProperties.getUserName(), ftpProperties.getPassword(), ftpProperties.getBasePath(), filePath, newFileName, multipartFile.getInputStream());
				if(res) {
					String http = ftpProperties.getHttpPath() + File.separator + filePath + File.separator + newFileName;
					@SuppressWarnings("unchecked")
					List<String> urlHttps =  (List<String>) result.getOrDefault("url", Arrays.asList());
					urlHttps.add(http);
					result.put("url", urlHttps);
					result.put("state", true);
					return result;
				}else {
					result.put("state", false);
				}
				
				return result;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

}
